package com.swlz.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.startsWith;

import java.sql.SQLException;
import com.swlz.dao.AreasDAO;
import com.swlz.entity.Areas;
import com.swlz.exception.CodeException;
import com.swlz.service.AreasService;
import com.swlz.vo.PageVO;

import cn.hutool.core.util.StrUtil;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 行政区域
 * ClassName : com.swlz.service.impl.AreasServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-27 12:15:25
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class AreasServiceImpl implements AreasService {
	
private static Logger log = LoggerFactory.getLogger(AreasServiceImpl.class);
	
	private AreasDAO areasDAO = new AreasDAO();

	@Override
	public int add(Areas areas) throws CodeException {
		try {
			return areasDAO.insertSelective(areas);
		} catch (SQLException e) {
			log.warn("添加行政区域错误",e);
			throw new CodeException(500, "添加行政区域错误",e);
		}
	}

	@Override
	public int update(Areas areas) throws CodeException {
		try {
			return areasDAO.updateSelective(areas);
		} catch (SQLException e) {
			log.warn("修改行政区域错误",e);
			throw new CodeException(501, "修改行政区域错误",e);
		}
	}

	@Override
	public int delete(String id) throws CodeException {
		try {
			return areasDAO.deleteById(id);
		} catch (SQLException e) {
			log.warn("删除行政区域错误",e);
			throw new CodeException(502, "删除行政区域错误",e);
		}
	}

	@Override
	public Areas get(String id) throws CodeException {
		try {
			return areasDAO.selectById(id);
		} catch (SQLException e) {
			log.warn("获取行政区域失败",e);
			throw new CodeException(503, "获取行政区域失败",e);
		}
	}

	@Override
	public PageVO<Areas> page(Areas areas, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = areasDAO.countByModel(areas);
			PageVO<Areas> page = new PageVO<>(total, current, rowMax);
			return page.setList(areasDAO.selectByModel(areas, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			log.warn("获取行政区域分页数据失败",e);
			throw new CodeException(504, "获取行政区域分页数据失败",e);
		}
	}

	@Override
	public List<Areas> list(Areas areas, int len) {
		try {
			return areasDAO.selectByModel(areas, 0, len);
		} catch (SQLException e) {
			log.warn("获取行政区域列表数据失败",e);
			throw new CodeException(505, "获取行政区域列表数据失败",e);
		}	
	}

	@Override
	public List<Areas> list(String id) {
		if(id==null){
			return null;
		}
		String regex = "^\\d+$";
		if(!id.matches(regex)){
			throw new CodeException(506,"格式不正确");
		}
		int len = id.length();
		StringBuffer ids = new StringBuffer();
		switch (len) {
		case 12:
			ids.append("'");
			ids.append(id);
			ids.append("'");
			ids.append(",");
		case 9:
			ids.append("'");
			ids.append(id.substring(0, 9));
			ids.append("'");
			ids.append(",");
		case 6:
			ids.append("'");
			ids.append(id.substring(0, 6));
			ids.append("'");
			ids.append(",");
		case 4:
			ids.append("'");
			ids.append(id.substring(0, 4));
			ids.append("'");
			ids.append(",");
		case 2:
			ids.append("'");
			ids.append(id.substring(0, 2));
			ids.append("'");
			break;
		default:
			throw new CodeException(506, "格式不正确");
		}
		try {
			log.debug(ids.toString());
			return areasDAO.selectByIds(ids.toString());
		} catch (SQLException e) {
			log.warn("获取行政区域列表数据失败",e);
			throw new CodeException(505, "获取行政区域列表数据失败",e);
		}	
	}

	@Override
	public String getName(String id) {
		StringBuffer name = new StringBuffer();
		for(Areas areas : list(id)){
			name.append(areas.getName());
		}
		return name.toString();
	}

	@Override
	public List<Areas> listChildren(String pid) {
		if(StrUtil.hasBlank(pid)){
			pid="0";
		}
		Areas areas= new Areas();
		areas.setPid(pid);
		try {
			return areasDAO.selectByModel(areas, 0, 999);
		} catch (SQLException e) {
			log.warn("获取行政区域列表数据失败",e);
			throw new CodeException(505, "获取行政区域列表数据失败",e);
		}	
	}


}
